package org.sipsl.gisystems.gentest.rte.util;

import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.xml.bind.DatatypeConverter;

public class GenericUtil {
	private static int level;
	
	public final static int CARRIAGE_RETURN = 0;
	public final static int NO_CARRIAGE_RETURN = 1;
	public final static int LEVEL_1 = 1;
	public final static int LEVEL_0 = 0;
	
	static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	static DateFormat timeFormat = new SimpleDateFormat("HHmm");
	static Date NOWTODAY = new Date();
	public final static String RTETODAY =  dateFormat.format(NOWTODAY);
	public final static String RTENOW =  timeFormat.format(NOWTODAY);

	public static String MakePrefix(int i){
		
		return (i<=9) ? "000"+i : (i<=99) ? "00"+i : (i<=999) ? "0"+i : ""+i;
	}
	public static void LOG(Object s, int _level,int carriage){
		
		if (_level == 1){
			if (carriage !=0){
				System.out.print(s);
			}else{
				System.out.println(s);
			}
		}
		else if (_level == 2){
			System.out.println("\n%"+ s+"%");
		}

	}
	
	public static void LOG(Object s){
		if (0 == level){
			System.out.println(s);
		}
	}
	public static void PICO(int line, int show){
		if (show!=0)
			System.out.println("Line ["+line+"]");

	}

	public static long ipToLong(String ipAddress) {
	    long result = 0;
	    String[] atoms = ipAddress.split("\\.");

	    for (int i = 3; i >= 0; i--) {
	        result |= (Long.parseLong(atoms[3 - i]) << (i * 8));
	    }

	    return result & 0xFFFFFFFF;
	}
	public static void setLevel(int level2) {
		level = level2;
		
	}
	public static long getBytes(String value) {
		

		if (value.endsWith("G")){
			Long myLong = new Long(value.replace("G", "")) *1024*1024*1024;
			return myLong;
		} 
		else if (value.endsWith("M")){
			Long myLong = new Long(value.replace("M", "")) * 1024*1024;
			return myLong;
		}
		else if (value.endsWith("K")){
			Long myLong = new Long(value.replace("K", "")) * 1024;
			return myLong;
		}else {
			Long myLong = new Long(value);
			return myLong;
		}
	}	
	public static String nullToEmpty(String o){
		if (o == null){
			return "";
		}else{
			return o;
		}
	}
	public static String parseDate(String date) {
		if( date.toUpperCase().startsWith("TODAY") ){
			if (date.toUpperCase().length() > 5){
				String[] mod = new String[2];
				
				int op = 1;

				mod = date.toUpperCase().split(":");
				Integer plus = new Integer(mod[1].substring(1));
				if (mod[1].startsWith("-")){
					op = -1;
				}
				
				Calendar cal = Calendar.getInstance();
				cal.setTime(NOWTODAY);   
				cal.add(Calendar.DATE, op* plus ); 
				return dateFormat.format(cal.getTime()); 
			}else{
				return dateFormat.format(NOWTODAY);
			}
		}else{
			return date; //unparsed
		}
	}
	
	public static int getUnitPow(String unit){
		if (unit.compareTo("B") == 0){
			return 0;
		}
		else if (unit.compareTo("K") == 0){
			return 1;
		}
		else if (unit.compareTo("M") == 0){
			return 2;
		}
		return 0;

	}
	public static long roundUp(long value, String unit){
		
		int unitUpp = 0;
		if (unit.compareTo("B") == 0){
			unitUpp = 3;
		}
		else if (unit.compareTo("K") == 0){
			unitUpp = 2;
		}
		else if (unit.compareTo("M") == 0){
			unitUpp = 1;
		}

		long result;
		double re = ((double) value) / (Math.pow(1024, 3-unitUpp));
		double re2 = Math.ceil(re);
		result = (long) re2;
		return result;
	}
	public static String parseTime(String time) {
		//Calendar.MINUTE
		if( time.toUpperCase().startsWith("NOW") ){
			if (time.toUpperCase().length() > 3){
				String[] mod = new String[2];
				
				int op = 1;

				mod = time.toUpperCase().split(":");
				Integer plus = new Integer(mod[1].substring(1));
				if (mod[1].startsWith("-")){
					op = -1;
				}
				
				Calendar cal = Calendar.getInstance();
				cal.setTime(NOWTODAY);   
				cal.add(Calendar.MINUTE, op* plus ); 
				return timeFormat.format(cal.getTime()); 
			}else{
				return timeFormat.format(NOWTODAY);
			}
		}else{
			return time; //unparsed
		}
	}
	public static String toIp(String sGSN) {
		if (sGSN.startsWith("0x")){
			InetAddress a = null ;
			try {
				a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary(sGSN.substring(2)));
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return a.getHostAddress();
		}else{
			return sGSN;
		}
	}
}
